// Fluxions 3D Engine
// bbox.hpp
// Copyright (C) 2000-2011 Jonathan Metzgar
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#ifndef FLUXIONS_BBOX_HPP
#define FLUXIONS_BBOX_HPP

#include <float.h>
#include <numeric>
#include "math.hpp"

namespace Fluxions
{
	class BBox
	{
	public:
		BBox();
		~BBox();
		
		void Reset();
		void operator +=(const Vector3f &point);
		bool IsPointInside(const Vector3f &point) const;
		float MaxXSize() const { return max.x - min.x; }
		float MaxYSize() const { return max.y - min.y; }
		float MaxZSize() const { return max.z - min.z; }
		
		Vector3f min;
		Vector3f max;
	};
	
	BBox::BBox()
		: min(FLT_MAX, FLT_MAX, FLT_MAX),
		max(-FLT_MAX, -FLT_MAX, -FLT_MAX)
	{
	}
	
	BBox::~BBox()
	{
	}
	
	void BBox::Reset()
	{
		max = Vector3f(-FLT_MAX, -FLT_MAX, -FLT_MAX);
		min = Vector3f(FLT_MAX, FLT_MAX, FLT_MAX);
	}
	
	void BBox::operator +=(const Vector3f &point)
	{
		min.x = std::min(point.x, min.x);
		min.y = std::min(point.y, min.y);
		min.z = std::min(point.z, min.z);
		max.x = std::max(point.x, max.x);
		max.y = std::max(point.y, max.y);
		max.z = std::max(point.z, max.z);
	}
	
	bool BBox::IsPointInside(const Vector3f &point) const
	{
		if (point.x < min.x || point.x > max.x)
			return false;
		if (point.y < min.y || point.y > max.y)
			return false;
		if (point.z < min.z || point.z > max.z)
			return false;
		return true;
	}
}

#endif
